package com.cskaoyan.service;


import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.vo.MallIssueVO;
import com.cskaoyan.module.bo.AfterIssueBO;
import com.cskaoyan.module.bo.AfterIssueCreateBO;
import com.cskaoyan.module.pojo.MarketAftersale;
import com.cskaoyan.module.pojo.MarketIssue;
import com.cskaoyan.module.pojo.MarketIssueExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :ligenghuisheng
 * @Description:
 * @Date :2022/4/7  20:49
 */
@Service
public class MallIssueServiceImpl implements MallIssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;
    @Override
    public MallIssueVO list(AfterIssueBO afterIssueBO, MarketIssue marketIssue) {
        //分页设置
        PageHelper.startPage(afterIssueBO.getPage(),afterIssueBO.getLimit());
        //创建example对象
        MarketIssueExample example = new MarketIssueExample();
        //使用这个createCriteria方法创建Example的内部类对象 → 里面维护了条件列表
        MarketIssueExample.Criteria criteria = example.createCriteria();
        MallIssueVO mallIssueVO = new MallIssueVO();

        PageInfo<MarketIssue> pageInfo = null;

        List<MarketIssue> marketIssues1 = null;
        List<MarketIssue> marketIssues2 = null;

        //查询question为空
        if(afterIssueBO.getQuestion()==null || afterIssueBO.getQuestion().equals("") ){
        marketIssues2 = marketIssueMapper.selectByExample(example);
        mallIssueVO.setList(marketIssues2);
        pageInfo = new PageInfo<>(marketIssues2);

        }else{
            criteria.andQuestionLike("%"+afterIssueBO.getQuestion()+"%");
            marketIssues1 = marketIssueMapper.selectByExample(example);
            mallIssueVO.setList(marketIssues1);
            pageInfo = new PageInfo<>(marketIssues1);

        }
        example.setOrderByClause(afterIssueBO.getSort()+" " +afterIssueBO.getOrder());
        int pageSize = pageInfo.getPageSize();
        long total = pageInfo.getTotal();

        mallIssueVO.setPage(afterIssueBO.getPage());
        mallIssueVO.setLimit(afterIssueBO.getLimit());
        mallIssueVO.setPages(pageSize);
        mallIssueVO.setTotal((int) total);

        return mallIssueVO;
    }

    @Override
    public MarketIssue create(AfterIssueCreateBO afterIssueCreateBO) {
        marketIssueMapper.create(afterIssueCreateBO.getQuestion(),afterIssueCreateBO.getAnswer());
        Integer id = afterIssueCreateBO.getId();
       MarketIssue marketIssue = marketIssueMapper.selectNewInsertById(id);
        return marketIssue;
    }

    @Override
    public MarketIssue update(MarketIssue marketIssue) {

        int i = marketIssueMapper.updateByPrimaryKey(marketIssue);
        return marketIssue;
    }

    @Override
    public void delete(MarketIssue marketIssue) {
        int i = marketIssueMapper.deleteByPrimaryKey(marketIssue.getId());

    }


}
